package org.raytracer.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.raytracer.algo.Scene;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Image;

public class MainWindow extends Shell {
	private Text filenameTxt;
	final private Canvas canvas;
	
	final private Scene scene=new Scene();

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			MainWindow shell = new MainWindow(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public MainWindow(Display display) {
		super(display, SWT.SHELL_TRIM);
		setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite.setLayout(new GridLayout(3, false));
		
		filenameTxt = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		filenameTxt.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
		filenameTxt.setEnabled(false);
		filenameTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Button importBtn = new Button(composite, SWT.NONE);
		importBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				scene.initScene("aa");
			}
		});
		importBtn.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		importBtn.setText("Import");
		
		Button startBtn = new Button(composite, SWT.NONE);
		startBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				//begin to render
				scene.render();
				canvas.redraw();
			}
		});
		GridData gd_startBtn = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_startBtn.widthHint = 60;
		startBtn.setLayoutData(gd_startBtn);
		startBtn.setText("Start");
		
		canvas = new Canvas(this, SWT.NONE);
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				if(scene.getImage()!=null){
					Image image = new Image(MainWindow.this.getDisplay(),scene.getImage());
					e.gc.drawImage(image, 0, 0);
					image.dispose();
				}
				
			}
		});
		canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("RayTracer");
		setSize(498, 449);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
